const tmp = require('./data.json');
const download = require('download');
const md5 = require('md5');
const fs = require('fs');
let key = Object.keys(tmp)[0];

const data = tmp[key];

const result = [];

data.forEach(item => {
    //声明
    let obj = {};
    //添加标题
    obj.title = item.title;
    //图片
    obj.cover = item.imgsrc;
    if(!obj.cover) return;
    //下载图片
    var dir = __dirname + '/../../public/images/news'
    var name = item.imgsrc.split('/').pop();
    //下载图片
    var path = dir + '/' + name;
    //下载图片
    download(obj.cover).pipe(fs.createWriteStream(path));
    //设置图片的路径
    obj.cover_local = '/images/news/' + name;
    //时间
    obj.ptime = item.ptime;
    //来源
    obj.from = item.source;
    //文档id
    obj.docid = item.docid;
    
    result.push(obj);
});

fs.writeFileSync(__dirname + '/news_10.json', JSON.stringify({news: result}));